//
//  AddAlarmClockNotification.m
//  AlarmClock
//
//  Created by apple on 16/10/13.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AddAlarmClockNotification.h"

@implementation AddAlarmClockNotification
+ (void)addAlarmClockNotification:(NSString *)alarmClockTime repeat:(NSString *)repeatType index:(NSInteger)index {
     NSMutableArray *array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"alarmClock"] mutableCopy];
    // 开始时间的小时和分钟
    NSArray *clockTimeArray = [alarmClockTime componentsSeparatedByString:@" : "];
    NSDate *dateNow = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    NSInteger calendarUnit = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitQuarter;
    
    dateComponents = [calendar components:calendarUnit fromDate:dateNow];
    // 设置小时、分钟、秒
    [dateComponents setHour:[clockTimeArray[0] integerValue]];
    [dateComponents setMinute:[clockTimeArray[1] integerValue]];
    [dateComponents setSecond:0];
    
    // 响铃类型为只响一次
    if ([repeatType isEqualToString:@"只响一次"]) {
        NSDate *newFireDate = [calendar dateFromComponents:dateComponents];
        int time = [[NSDate new] timeIntervalSinceDate:newFireDate];
        if (time > 0) {
            newFireDate = [newFireDate dateByAddingTimeInterval:3600 * 24];
        }
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        if (notification) {
            notification.fireDate = newFireDate;
            notification.alertBody = @"您设置的时间已经到了！！";
            notification.soundName = @"ring.wav";
            notification.repeatInterval = NSCalendarUnitWeekOfYear;
            notification.timeZone = [NSTimeZone defaultTimeZone];
            notification.alertLaunchImage = @"timer_btn_ring_normal.png";
            if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.2) {
                notification.alertTitle = @"时间到了！！";
            }
            notification.applicationIconBadgeNumber = 1;
            NSDictionary *dic = array[index];
            notification.userInfo = dic;
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }
        NSLog(@"add:%@", notification.fireDate);
    }
    // 响铃类型为循环周期
    else {
        // 今天是星期几
        NSInteger weekday = [dateComponents weekday];
        // 选择了哪些天（星期    日、一、二、三、四、五、六）
        NSArray *array = [repeatType componentsSeparatedByString:@" "];
        // 选择的星期距离今天多少天
        NSInteger days = 0;
        // 选择了的天数
        NSInteger daysNumber = array.count;
        // 选择的星期数对应的index（1、2、3、4、5、6、7）
        NSInteger clockDays[7];
        
        NSArray *weekdays = @[@"星期日",
                              @"星期一",
                              @"星期二",
                              @"星期三",
                              @"星期四",
                              @"星期五",
                              @"星期六"];
        
        // 查找设定的周期模式
        for (int i = 0; i < daysNumber; i ++) {
            for (int j = 0; j < 7; j ++) {
                if ([array[i] isEqualToString:weekdays[j]]) {
                    clockDays[i] = j + 1;
                    break;
                }
            }
        }
        
        for (int i = 0; i < daysNumber; i ++) {
            days = clockDays[i] >= weekday ? clockDays[i] - weekday : clockDays[i] - weekday + 7;
            NSDate *newFireDate = [[calendar dateFromComponents:dateComponents] dateByAddingTimeInterval:3600 * 24 * days];
            
            UILocalNotification *notification = [[UILocalNotification alloc] init];
            if (notification) {
                notification.fireDate = newFireDate;
                notification.alertBody = @"您设置的时间已经到了！！";
                notification.soundName = @"ring.wav";
                notification.repeatInterval = NSCalendarUnitWeekOfYear;
                notification.timeZone = [NSTimeZone defaultTimeZone];
                notification.alertLaunchImage = @"timer_btn_ring_normal.png";
                if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.2) {
                    notification.alertTitle = @"时间到了！！";
                }
                notification.applicationIconBadgeNumber = 1;
                //notification.userInfo = [NSDictionary dictionaryWithObject:clockID forKey:@"clockID"];
                [[UIApplication sharedApplication] scheduleLocalNotification:notification];
            }
            NSLog(@"add:%@", notification.fireDate);
        }
    }

}
@end
